package com.base.api.bms.impl;

import com.alibaba.fastjson.JSON;
import com.base.api.bms.IMipsSettlementUploadService;
import com.base.dao.bms.MipsSettlementUploadMapper;
import com.base.dao.bms.VMipsSettleUploadDetialMapper;
import com.base.utils.ResultUtil;
import com.base.vo.bms.MipsSettlementUpload;
import com.base.vo.bms.VMipsSettleUploadDetial;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IMipsSettlementUploadImpl implements IMipsSettlementUploadService {
    @Autowired
    private MipsSettlementUploadMapper mipsSettlementUploadMapper;
    @Autowired
    private VMipsSettleUploadDetialMapper vMipsSettleUploadDetialMapper;

    @Override
    public String find(String yblx,String patiendId, String startDate, String endDate,String pageNum, String pageSize, String order, String sort) {
        if (pageNum == null) {
            pageNum = "1";
        }
        if (pageSize == null) {
            pageSize = "50";
        }
        if (order == null) {
            order = "id";
        }
        if (sort == null) {
            sort = "desc";
        }
        int pageNum1 = Integer.parseInt(pageNum);
        int pageSize1 = Integer.parseInt(pageSize);
        PageHelper.startPage(pageNum1,pageSize1);
        List<VMipsSettleUploadDetial> list = vMipsSettleUploadDetialMapper.selectAll(yblx,patiendId, startDate,endDate, order, sort);
        PageInfo<VMipsSettleUploadDetial> pageInfo = new PageInfo(list);
        Map<String, Object> map = new HashMap<>();
        map.put("count", pageInfo.getTotal());
        map.put("data", pageInfo.getList());
        map.put("code", 0);
        map.put("msg", "");
        return JSON.toJSONString(map);
    }

    @Override
    public String save(MipsSettlementUpload mipsSettlementUpload) {
        if(mipsSettlementUpload.getId()==null){
            if(mipsSettlementUploadMapper.insert(mipsSettlementUpload) > 0){
                return   ResultUtil.success();
            }else {
                return ResultUtil.error("系统异常,保存单病种上报结果失败!");
            }
        } else {
            if(mipsSettlementUploadMapper.updateByPrimaryKeySelective(mipsSettlementUpload) > 0){
                return   ResultUtil.success();
            }else {
                return ResultUtil.error("系统异常,撤销单病种上报结果失败!");
            }
        }

    }
}
